package datascope.scan;

import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.Statements;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by TD on 2021/11/2
 */
public class Cache {

    private static final Logger log = LoggerFactory.getLogger(Cache.class);

    private static Map<String, DataScopeProperty> O000O0oO;

    public Cache() {
    }

    public static void O0000oo0() {
        O000O0oO = new ConcurrentHashMap();
    }

    public static DataScopeProperty O0000oOO(String var0) throws Exception {
        DataScopeProperty var1 = O000O0oO.get(var0);
        if (null == var1) {
            String var2 = name(var0); // 获取名称
            if (null == var2 || DataScopeProperty.CHECK_INSTANCE == O000O0oO.get(var2)) {
                return null;
            }

            Class var3 = Class.forName(var2);
            Method[] var4 = var3.getMethods();
            int var5 = var4.length;
            // 反射获取方法上注解
            for (int var6 = 0; var6 < var5; ++var6) {
                Method var7 = var4[var6];
                DataScope var8 = (DataScope) var7.getAnnotation(DataScope.class);
                if (null != var8) {
                    O000O0oO.put(var2 + "." + var7.getName(), new DataScopeProperty(var8));
                }
            }
            // 查找类上注解
            DataScope var9 = (DataScope) var3.getAnnotation(DataScope.class);
            O000O0oO.put(var2, null == var9 ? DataScopeProperty.CHECK_INSTANCE : new DataScopeProperty(var9));
            var1 = (DataScopeProperty) O000O0oO.get(var0);
        }

        return var1;
    }

    public static void processStatements(Object[] var0, MappedStatement var1, AAA var2) {
        BoundSql var3 = var1.getBoundSql(var0[1]);
        String var4 = var3.getSql();
        log.info("original SQL: " + var4);
        try {
            StringBuffer var5 = new StringBuffer();
            Statements var6 = CCJSqlParserUtil.parseStatements(var4);
            int var7 = 0;

            for (Iterator var8 = var6.getStatements().iterator(); var8.hasNext(); ++var7) {
                Statement var9 = (Statement) var8.next();
                if (var7 > 0) {
                    var5.append(";");
                }

                var2.process(var9, var7);
                var5.append(var9);
            }

            MappedStatement mappedStatement = BBB.O000000o(var1, new BoundSql(var1.getConfiguration(), var5.toString(), var3.getParameterMappings(), var3.getParameterObject()));
            var0[0] = mappedStatement;
            log.info("dataScope execute sql: {}", var5);

        } catch (JSQLParserException var10) {
            throw ExceptionUtils.mpe("Failed to process, Error SQL: %s", var10.getCause(), new Object[]{var4});
        }
    }

    public static String name(String var0) {
        int var1 = var0.lastIndexOf(".");
        return var1 > 0 ? var0.substring(0, var1) : null;
    }

}
